package scau.jump.securitytrace.listener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

import scau.jump.securitytrace.init.Init;
import scau.jump.securitytrace.popupwindow.UserWindow;

import com.securitytrace.model.User;

public class UpdateUserButtonListener implements ActionListener {

	private UserWindow userWindow;
	private User user;
	
	public UpdateUserButtonListener(UserWindow userWindow) {
		this.userWindow = userWindow;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		String password = userWindow.getUserPasswordTextField().getText();
		if(password.trim().isEmpty()) {
			JOptionPane.showMessageDialog(null, "密码不能为空");
		}else {
			user.setPassword(password);
			try {
				 updateUserToDatabase(user.getId(),password);
				 userWindow.getManagerClient().setResult(user);
				 userWindow.dispose();
			} catch(Exception ex) {
				JOptionPane.showMessageDialog(userWindow, "操作失败");
			}
		}
	}

	private void updateUserToDatabase(long id, String password) {
		Init.getUserService().updatePassword(id, password);
		System.out.println("update user by id=" + id);
	}

	/**以下为 get/set 方法**/
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
}
